//
//  CollectionViewController.m
//  liwo
//
//  Created by liuwenyang on 14-9-19.
//  Copyright (c) 2014年 liuwenyang. All rights reserved.
//

#import "LBEasyCollectionController.h"
#import "MJRefresh.h"
typedef NS_ENUM(NSUInteger, CollectionHeaderOrFooter) {
    Header = 0,
    Footer
};
@interface LBEasyCollectionController()



@end


@implementation LBEasyCollectionController


-(instancetype)initWithAutoLayout
{
    self = [super init];
    if (self) {

        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _flowLayout.minimumLineSpacing = 0;
        _flowLayout.minimumInteritemSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_flowLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }
    
    return self;
}


-(instancetype)initWithCellForItem:(Class)classForItem
{
    self = [self initWithAutoLayout];
    if (self) {
        
        _classForItem = classForItem;
        BOOL isUseNib = [_classForItem isUseNib];
        if (isUseNib) {
            [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass(_classForItem) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass(_classForItem)];
        }else{
            [_collectionView registerClass:_classForItem forCellWithReuseIdentifier:NSStringFromClass(_classForItem)];
        }
    }
    
    return self;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    id data = [_dataSource objectAtIndex:indexPath.row];
    Class cellClass = _classForItem;
    return [cellClass sizeForItem:data indexPath:indexPath totalListNumber:self.dataSource.count];
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataSource.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LBEasyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(_classForItem) forIndexPath:indexPath];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(beforeConfigWithCollectionController:collectionCell:data:)]) {
        [self.delegate beforeConfigWithCollectionController:self collectionCell:cell data:[_dataSource objectAtIndex:indexPath.row]];
    }
    
    [cell configCellWithData:[_dataSource objectAtIndex:indexPath.row] atIndexPath:indexPath totalListNumber:self.dataSource.count];
    cell.itemDelegate = self;
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectedCell:dataSrc:index:)]) {
        [self.delegate didSelectedCell:self dataSrc:[self.dataSource objectAtIndex:indexPath.row] index:indexPath];
    }
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(void)clickElement:(LBEasyCollectionViewCell *)collectionViewCell element:(id)element ItemData:(id)itemData
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(didClickElementForItem:element:data:)]) {
        [self.delegate didClickElementForItem:self element:element data:itemData];
    }
}


-(void)dealloc
{
    NSLog(@"%@ dealloc",NSStringFromClass([self class]));
}

@end

@interface LBEasyRefleshCollectionController ()


@property(nonatomic,assign)BOOL enableInfiniteScroll;



@end

@implementation LBEasyRefleshCollectionController



-(id)initWithDragRefresh:(BOOL)enableDragRefresh withInfiniteScroll:(BOOL)enableInfiniteScroll classForItem:(Class)classForItem
{
    self = [super initWithCellForItem:classForItem];
    if (self) {
        if (enableDragRefresh) {
            self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(forceRefresh)];
        }
        _enableInfiniteScroll = YES;
        self.pageIndex = 1;
    }
    return self;
}


- (void)forceRefresh
{
    [self refreshHeaderViewDidCallBackWithAnimation:YES];
}

-(void)forceRefreshWithoutAnimation
{
    [self refreshHeaderViewDidCallBackWithAnimation:NO];
}


#pragma mark - 顶部的重新刷新
#pragma mark RefreshHeaderViewDelegate

- (void)refreshHeaderViewDidCallBackWithAnimation:(BOOL)animation
{
    if (animation) {
        [self.collectionView.mj_header beginRefreshing];
    }
    __weak LBEasyRefleshCollectionController *weakSelf = self;

    self.pageIndex = 1;
    [self refreshTable:^(BOOL finished) {
        if (finished) {
            
            [weakSelf finishLoadingAndResetFooter:Header];
            
        } else {
            //加入空白页或者错误页
            NSLog(@"modelDidLoadFailure");
        }
    }];
}

#pragma mark - 底部的获取更多
#pragma mark RefreshFooterViewDelegate

- (void)refreshFooterViewDidCallBack
{
    __weak LBEasyRefleshCollectionController *weakSelf = self;
    ++self.pageIndex;
    [self getTableMore:^(BOOL finished) {
        if (finished) {
            
            [weakSelf finishLoadingAndResetFooter:Footer];
            
        }else{
            //加入空白页或者错误页
            NSLog(@"modelDidLoadFailure");
        }
        
    }];
}


-(void)getTableMore:(void (^)(BOOL))block
{
    
}

-(void)refreshTable:(void (^)(BOOL))block
{
    
}

-(void)finishLoadingAndResetFooter:(CollectionHeaderOrFooter)headerOrFooter
{
    
    if (headerOrFooter == Header) {
        [self.collectionView.mj_header endRefreshing];
    }else{
        [self.collectionView.mj_footer endRefreshing];
    }
    
    
    if (self.enableInfiniteScroll) {
        if (self.totalCount > self.dataSource.count || self.totalPages > self.pageIndex) {
            //需要展现加载更多
//            if (self.collectionView.mj_footer == nil) {
            self.collectionView.mj_footer  = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshFooterViewDidCallBack)];
//            }
        }else{
            [self.collectionView.mj_footer removeFromSuperview];
            self.collectionView.mj_footer = nil;
        }
    }
    
    
    //通知代理model加载完成
    if (self.delegate && [self.delegate respondsToSelector:@selector(modelDidLoadFinished:)]) {
        [self.delegate modelDidLoadFinished:self];
    }
}


@end




















